package com.tanhua.manager.api.db;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.domain.db.AnalysisByDay;
import com.tanhua.domain.vo.AnalysisSummaryVo;
import com.tanhua.domain.vo.AnalysisUsersVo;
import com.tanhua.domain.vo.DataPointVo;
import com.tanhua.mapper.AnalysisByDayMapper;
import com.tanhua.util.ComputeUtil;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AnalysisApiImpl implements AnalysisApi {

    @Autowired
    private AnalysisByDayMapper analysisByDayMapper;

    @Override
    public AnalysisSummaryVo summary() {
        // 声明vo
        AnalysisSummaryVo vo = new AnalysisSummaryVo();

        // 今天
        Date date = new Date();
        String today = DateUtil.format(date, "yyyy-MM-dd");

        // 昨天
        String yesterday = ComputeUtil.offsetDay(date, -1);

        // 1、累计用户
        Long cumulativeUsers = analysisByDayMapper.selectCumulativeUsers();
        vo.setCumulativeUsers(cumulativeUsers);
        // 2、过去7天活跃用户
        Long num7 = analysisByDayMapper.selectNumActive(ComputeUtil.offsetDay(date, -7), today);
        vo.setActivePassWeek(num7);
        // 3、过去30天活跃用户
        Long num30 = analysisByDayMapper.selectNumActive(ComputeUtil.offsetDay(date, -30), today);
        vo.setActivePassMonth(num30);
        // 4、今日新增用户
        QueryWrapper<AnalysisByDay> todayQuery = new QueryWrapper<>();
        todayQuery.eq("record_date", today);
        AnalysisByDay todayAna = analysisByDayMapper.selectOne(todayQuery);
        vo.setNewUsersToday(todayAna.getNumRegistered().longValue());
        // 5、昨日新增用户对比
        QueryWrapper<AnalysisByDay> yesQuery = new QueryWrapper<>();
        yesQuery.eq("record_date", yesterday);
        AnalysisByDay yesAna = analysisByDayMapper.selectOne(yesQuery);
        vo.setNewUsersTodayRate(ComputeUtil.computeRate(todayAna.getNumRegistered(), yesAna.getNumRegistered()));

        // 6、今日登录次数
        vo.setLoginTimesToday(todayAna.getNumLogin().longValue());

        // 7、昨日登录次数对比
        vo.setLoginTimesTodayRate(ComputeUtil.computeRate(todayAna.getNumLogin(), yesAna.getNumLogin()));
        // 8、今日活跃用户
        vo.setActiveUsersToday(todayAna.getNumActive().longValue());

        // 9、昨日活跃用户对比
        vo.setActiveUsersTodayRate(ComputeUtil.computeRate(todayAna.getNumActive(), yesAna.getNumActive()));
        // 返回vo
        return vo;
    }

    @Override
    public AnalysisUsersVo users(Long sd, Long ed, Integer type) {
        AnalysisUsersVo vo = new AnalysisUsersVo();

        // 开始时间
        DateTime startDate = DateUtil.date(sd);
        // 结束时间
        DateTime endDate = DateUtil.date(ed);

        // 今年
        vo.setThisYear(findDataPointVoList(startDate, endDate, type));
        // 去年
        vo.setLastYear(
                findDataPointVoList(
                        DateUtil.offset(startDate, DateField.YEAR, -1),
                        DateUtil.offset(endDate, DateField.YEAR, -1),
                        type)
        );
        return vo;
    }

    // 查询指定日期范围内的统计数据
    private List<DataPointVo> findDataPointVoList(DateTime startDate, DateTime endDate, Integer type) {
        List<DataPointVo> voList = new ArrayList<>();

        String column = null;
        switch (type) {
            case 101: {
                column = "num_registered";
                break;
            }
            case 102: {
                column = "num_active";
                break;
            }
            case 103: {
                column = "num_retention1d";
                break;
            }
        }
        // 自定义sql拼接
        QueryWrapper<AnalysisByDay> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("record_date," + column + " as num_active");
        queryWrapper.ge("record_date", startDate);
        queryWrapper.le("record_date", endDate);
        List<AnalysisByDay> byDayList = analysisByDayMapper.selectList(queryWrapper);
        for (AnalysisByDay byDay : byDayList) {
            DataPointVo vo = new DataPointVo();
            vo.setTitle(DateUtil.format(byDay.getRecordDate(), "yyyy-MM-dd"));
            vo.setAmount(byDay.getNumActive().longValue());
            voList.add(vo);
        }
        return voList;
    }

}
